import { Controller, Post } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { CrudRequest, QueryOptions, GetManyDefaultResponse } from '@crudjs/crud';

import { User } from './user.entity';
import { UsersService } from './users.service';
import { creatController, CrudPlus } from '@crudjs/crud-plus';

const queryOptions: QueryOptions = {
  softDelete: true,
  join: {
    company: {
      exclude: ['description'],
    },
    'company.projects': {
      alias: 'pr',
      exclude: ['description'],
    },
    profile: {
      eager: true,
      exclude: ['updatedAt'],
    },
  },
};
@CrudPlus({
  model: {
    type: User,
  },
  routes: {
    exclude: [
      'replaceOneBase',
      'createManyBase',
      'createOneBase',
      'deleteOneBase',
      'recoverOneBase',
      'updateOneBase',
    ],
  },
  params: {
    companyId: {
      field: 'companyId',
      type: 'number',
    },
    id: {
      field: 'id',
      type: 'number',
      primary: true,
    },
  },
  query: queryOptions,
})
@ApiTags('users-plus')
@Controller('/plus/companies/:companyId/users')
export class UsersPlusController extends creatController(User, {
  exclude: ['replaceOneBase'],
}) {
  constructor(public service: UsersService) {
    super(service);
    // this.replaceOneBase // error
  }

  @ApiResponse({ type: String })
  @ApiOperation({})
  async getManyBase(req: CrudRequest): Promise<User[] | GetManyDefaultResponse<User>> {
    const res = await super.getManyBase(req);
    return ('Override route by Override method:\n' + JSON.stringify(res)) as any;
  }
  @ApiOperation({
    description: 'The description is set and the original summary takes effect',
  })
  getOneBase(req: CrudRequest): Promise<User> {
    return Promise.all([super.getOneBase(req), this.getManyBase(req)]) as any;
  }
  @Post()
  @ApiOperation({
    summary: 'the route created entirely by hand',
  })
  createOneBase(req: CrudRequest, dto: User): Promise<User> {
    return super.createOneBase(req, dto);
  }
}
